﻿#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>
#include <atlstr.h>

// https://zhuanlan.zhihu.com/p/75900393
#ifdef WIN32
void __sleep(int ms) {
    Sleep(ms);
}
#else
#include <unistd.h>
void __sleep(int ms) {
    usleep(1000L * ms);
}
#endif // WIN32

using namespace std;

// https://zhuanlan.zhihu.com/p/33757669
BOOL CopyToClipboard(const char* pszData)
{
    if (::OpenClipboard(NULL))
    {
        ::EmptyClipboard();
        HGLOBAL clipbuffer;
        char* buffer;
        clipbuffer = ::GlobalAlloc(GMEM_DDESHARE, strlen(pszData) + 1);
        buffer = (char*)::GlobalLock(clipbuffer);
        strcpy_s(buffer, strlen(pszData) + 1, pszData);
        ::GlobalUnlock(clipbuffer);
        ::SetClipboardData(CF_TEXT, clipbuffer);
        ::CloseClipboard();
        return TRUE;
    }
    return FALSE;
}


bool running = true;

string filename = "C:\\test.txt";
int s0 = 5000; // initial delay
int s1 = 1000; // delay between each operation
int s2 = 2500; // delay after status change from not running to running
int s3 = 50;   // delay between keyboard listening
int s4 = 100;  // delay between ctrl + v and enter

bool changeStatus = false;

void sleepAndLookupKey(int sleeptime) {
    for (int i = 0; i <= sleeptime / s3; i++) {
        int ch;
        if (_kbhit()) {
            ch = _getch();
            if ((running && ch == ' ') || (!running && ch == '`')) {
                changeStatus = true;
            }
        }
        __sleep(s3);
    }
}

int main() {

    CString cstring;

    system("pause");

    GetPrivateProfileString(TEXT("config"), TEXT("filename"), TEXT("C:\\test.txt"), cstring.GetBuffer(MAX_PATH), MAX_PATH, TEXT(".\\config.ini"));
    filename = (CStringA)cstring;
    s0 = GetPrivateProfileInt(TEXT("config"), TEXT("s0"), s0, TEXT(".\\config.ini"));
    s1 = GetPrivateProfileInt(TEXT("config"), TEXT("s1"), s1, TEXT(".\\config.ini"));
    s2 = GetPrivateProfileInt(TEXT("config"), TEXT("s2"), s2, TEXT(".\\config.ini"));
    s3 = GetPrivateProfileInt(TEXT("config"), TEXT("s3"), s3, TEXT(".\\config.ini"));
    s4 = GetPrivateProfileInt(TEXT("config"), TEXT("s4"), s4, TEXT(".\\config.ini"));

    ifstream f;
    f.open(filename);
    string s;

    cout << "程序将于"<<(1.0*s0/1000.0)<<"秒后开始运行"<<endl;
    cout << "文件：" << filename << endl;
    __sleep(s0);

    while (1) {

        int sleepTime = s3; // sleep time of this round, running -> s1, not running -> s3
        changeStatus = false;

        if (running) {
            if (!getline(f, s)) {
                break;
            };
            cout << s << endl;
            CopyToClipboard(s.c_str());
            keybd_event(VK_CONTROL, 0, 0, 0);
            keybd_event('V', 0, 0, 0);
            keybd_event('V', 0, KEYEVENTF_KEYUP, 0);
            keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);

            sleepAndLookupKey(s4);

            keybd_event(VK_RETURN, 0, 0, 0);
            keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);

            sleepTime = s1;
        }

        
        sleepAndLookupKey(sleepTime);

        if (changeStatus) {
            running = !running;

            cout << "状态：";
            if (running) {
                cout << "开始，将在" << (1.0 * s2 / 1000.0) << "秒后开始运行" << endl;
                __sleep(s2);
            }
            else {
                cout << "暂停" << endl;
            }
        }

    }
    return 0;
}
